package com.szholly.plug.activity.model;

import java.util.HashMap;

import org.springframework.stereotype.Component;

import com.szholly.utils.spring.SpringBeanFactory;
import com.szholly.data.general.*;
import com.szholly.data.general.metadata.IBaseTable;

/**
 * 定义工作流审批历史信息表
 */
@Component("BPM_HISTORY")
public class BpmHistoryTable extends InterceptorTableBase  implements IBaseTable{

	public final static String TABLE_NAME = "BPM_HISTORY";
	
	/**
	 * single ref
	 */
	public static BpmHistoryTable getSingleRef() {
		return (BpmHistoryTable) SpringBeanFactory.getBean("BPM_HISTORY");
	}

	@Override
	public String getMetadataTableName() {
		return TABLE_NAME;
	}

	@Override
	public String getMetadataTableAliasName() {
		return "审批历史表";
	}

	private HashMap<String, Field> fields;

	@Override
	protected HashMap<String, Field> fields() {
		if (fields == null) {
			fields = new HashMap<String, Field>();

			Field itemField = new Field();
			itemField.setFieldName(RowBase.FIELD_GUID);
			itemField.setDataType(GlobalDataType.String);
			itemField.setFieldID(1);
			itemField.setFieldAliasName("GUID");
			itemField.setCanShow(false);
			itemField.setIsKey(true);
			fields.put(itemField.getFieldName().toUpperCase().trim(), itemField);
			
			itemField = new Field();
			itemField.setFieldName(RowBase.FIELD_VERSION);
			itemField.setDataType(GlobalDataType.Int32);
			itemField.setFieldID(2);
			itemField.setFieldAliasName("VERSION");
			fields.put(itemField.getFieldName().toUpperCase().trim(), itemField);
			
			itemField = new Field();
			itemField.setFieldName(BpmHistoryRow.FIELD_SOptiontime);
			itemField.setDataType(GlobalDataType.DateTime);
			itemField.setFieldID(5);
			itemField.setFieldAliasName("审批时间");
			itemField.setCanShow(false);
			fields.put(itemField.getFieldName().toUpperCase().trim(), itemField);
			
			itemField = new Field();
			itemField.setFieldName(BpmHistoryRow.FIELD_SOptioncontent);
			itemField.setDataType(GlobalDataType.String);
			itemField.setFieldID(6);
			itemField.setFieldAliasName("审批内容(审批意见)");
			itemField.setCanShow(false);
			fields.put(itemField.getFieldName().toUpperCase().trim(), itemField);
			
			itemField = new Field();
			itemField.setFieldName(BpmHistoryRow.FIELD_ASSIGNEES);
			itemField.setDataType(GlobalDataType.String);
			itemField.setFieldID(7);
			itemField.setFieldAliasName("审核人");
			itemField.setCanShow(false);
			fields.put(itemField.getFieldName().toUpperCase().trim(), itemField);
			
			itemField = new Field();
			itemField.setFieldName(BpmHistoryRow.FIELD_ASSIGNEESID);
			itemField.setDataType(GlobalDataType.String);
			itemField.setFieldID(7);
			itemField.setFieldAliasName("审核人ID");
			itemField.setCanShow(false);
			fields.put(itemField.getFieldName().toUpperCase().trim(), itemField);
			
			itemField = new Field();
			itemField.setFieldName(BpmHistoryRow.FIELD_SExeid);
			itemField.setDataType(GlobalDataType.String);
			itemField.setFieldID(8);
			itemField.setFieldAliasName("流程实例");
			itemField.setCanShow(false);
			fields.put(itemField.getFieldName().toUpperCase().trim(), itemField);
			
			itemField = new Field();
			itemField.setFieldName(BpmHistoryRow.FIELD_SStakname);
			itemField.setDataType(GlobalDataType.String);
			itemField.setFieldID(9);
			itemField.setFieldAliasName("流程节点名称");
			itemField.setCanShow(false);
			fields.put(itemField.getFieldName().toUpperCase().trim(), itemField);
			
			itemField = new Field();
			itemField.setFieldName(BpmHistoryRow.FIELD_STAKID);
			itemField.setDataType(GlobalDataType.String);
			itemField.setFieldID(9);
			itemField.setFieldAliasName("流程节点ID");
			itemField.setCanShow(false);
			fields.put(itemField.getFieldName().toUpperCase().trim(), itemField);
			
			itemField = new Field();
			itemField.setFieldName(BpmHistoryRow.FIELD_NextStakname);
			itemField.setDataType(GlobalDataType.String);
			itemField.setFieldID(9);
			itemField.setFieldAliasName("下一流程节点名称");
			itemField.setCanShow(false);
			fields.put(itemField.getFieldName().toUpperCase().trim(), itemField);
			
			itemField = new Field();
			itemField.setFieldName(BpmHistoryRow.FIELD_SOption);
			itemField.setDataType(GlobalDataType.String);
			itemField.setFieldID(10);
			itemField.setFieldAliasName("审核操作(通过，不通过)");
			itemField.setCanShow(false);
			fields.put(itemField.getFieldName().toUpperCase().trim(), itemField);
		}
		return fields;
	}

	@Override
	public String getTableGroup() {
		return "工作流";
	}
	
	public void SaveEntityInThread(BpmHistoryRow entity){
//		HistoryThread logThread = new HistoryThread(this, entity);
//		logThread.start();
		save(entity);
	}
	
	@SuppressWarnings("unused")
	private class HistoryThread extends Thread {

		private BpmHistoryTable service;
		private BpmHistoryRow entity;
		
		public HistoryThread(BpmHistoryTable service, BpmHistoryRow entity){
			this.service = service;
			this.entity = entity;
		}
		
		@Override
		public void run() {
			service.save(entity);
		}
	}
}